<?php

namespace app\common\model;

use think\facade\Db;
use think\Model;

class StudyReport extends Model
{
    //自动生成写入时间
    protected $autoWriteTimestamp = true;

    public function study_report($data)
    {
        if (empty($data['time'])) {
            if (empty($data['classes_id'])) {
                if(empty($data['student_id'])){
                    return Db::table('study_report')
                        ->alias('s')
                        ->join('student stu', 's.student_id = stu.id')
                        ->field('s.*,stu.name as student_name')
                        ->order('create_time', 'desc')
                        ->paginate($data);
                }else{
                    return Db::table('study_report')
                        ->alias('s')
                        ->join('student stu', 's.student_id = stu.id')
                        ->field('s.*,stu.name as student_name')
                        ->where('s.student_id' ,'=' ,$data['student_id'])
                        ->order('create_time', 'desc')
                        ->paginate($data);
                }

            } else {
                return Db::table('study_report')
                    ->alias('s')
                    ->join('student stu', 's.student_id = stu.id')
                    ->field('s.*,stu.name as student_name')
                    ->where('stu.classes_id' ,'=' ,$data['classes_id'])
                    ->order('create_time', 'desc')
                    ->paginate($data);
            }
        } else {
            if (empty($data['classes_id'])) {
                return Db::table('study_report')
                    ->alias('s')
                    ->join('student stu', 's.student_id = stu.id')
                    ->field('s.*,stu.name as student_name')
                    ->whereDay('s.create_time', $data['time'])
                    ->order('create_time', 'desc')
                    ->paginate($data);
            } else {
                return Db::table('study_report')
                    ->alias('s')
                    ->join('student stu', 's.student_id = stu.id')
                    ->field('s.*,stu.name as student_name')
                    ->whereDay('s.create_time', $data['time'])
                    ->where('stu.classes_id' ,'=' ,$data['classes_id'])
                    ->order('create_time', 'desc')
                    ->paginate($data);
            }
        }

    }

}